import os
import keras
from keras.applications import inception_v3 as inc_net
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions
from skimage.io import imread
import matplotlib.pyplot as plt
#%matplotlib inline
import numpy as np
from skimage.segmentation import mark_boundaries
import sys
sys.path.append("..")# allow the notebook to find the parent folder
#from lime.lime_image import *
from lime import lime_image
print('Notebook run using keras:', keras.__version__)
print(sys.path)
#Here we create a standard InceptionV3 pretrained model
#and use it on images by first preprocessing them with the preprocessing tools
inet_model = inc_net.InceptionV3()
def transform_img_fn(path_list):
out = []
for img_path in path_list:
img = image.load_img(img_path, target_size=(299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = inc_net.preprocess_input(x)
out.append(x)
return np.vstack(out)
images = transform_img_fn([os.path.join('..\data','forest_cat.jpg')])
# I'm dividing by 2 and adding 0.5 because of
# how this Inception represents images
plt.imshow(images[0] / 2 + 0.5)
plt.show()
preds = inet_model.predict(images)
for x in decode_predictions(preds)[0]:
print(x)
explainer = lime_image.LimeImageExplainer(feature_selection='none')#kernel_width=0.1 feature_selection='none'
# Hide color is the color for a superpixel turned OFF. Alternatively, if it is NONE, the superpixel will be replaced by the average of its pixels
explanation = explainer.explain_instance(images[0], inet_model.predict,
top_labels=1, hide_color=0, batch_size=10,
num_samples=100,model_regressor='non_Bay')#'non_Bay' 'Bay_non_info_prior' 'Bay_info_prior'
# temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=2, hide_rest=True)
# plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
# plt.show()
# temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=5, hide_rest=False)
# plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
# plt.show()
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=8, hide_rest=False)
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
plt.show()
import timeit
import time
## this part is used to capture return value with Python timeit module
##cf. https://stackoverflow.com/questions/24812253/how-can-i-capture-return-value-with-python-timeit-module
## A warmup example...
def f(x,y):
a=x*y+10
return a
x = 5
y = 7
print(timeit.timeit(stmt='f(x,y)',
setup='from __main__ import f, x, y',
number=100))
def the_LIME_interpretor(n_samples):
explainer = lime_image.LimeImageExplainer(feature_selection='none')#kernel_width=0.1 feature_selection='none'
explanation = explainer.explain_instance(images[0], inet_model.predict,
top_labels=1, hide_color=0, batch_size=10,
num_samples=n_samples,model_regressor='non_Bay')#'non_Bay' 'Bay_non_info_prior' 'Bay_info_prior'
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=8, hide_rest=False)
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
plt.show()
return
LIME_time=np.array([])
i=5
while i<=1500:
n_samples=i
temp=timeit.timeit(stmt='the_LIME_interpretor(n_samples)',
setup='from __main__ import the_LIME_interpretor, n_samples',
number=1)
#print(temp[0], temp[1][0],temp[1][1])
LIME_time=np.append(LIME_time,temp)
i=i+55
i=5
x_index=np.array([])
while i<=1500:
x_index =np.append(x_index,i)
i=i+55
## Above is to rescale the x axis; Note need to have the same setup with the loop above...
plt.plot(x_index,LIME_time,linestyle='-',color='green')
#plt.plot(x_index[w:],IPSP_est_upper[w:],linestyle='dotted',color='purple',label='IPSP upper bound')
#plt.axis([1, len(MCMC_time), 0, np.max(MCMC_time)])# set the ranges of axis
#plt.legend(fontsize=9,bbox_to_anchor=(1,1))
plt.xlabel('sample size $n$')
plt.ylabel('time (s)')
plt.grid(True)
plt.show()
images = transform_img_fn([os.path.join('..\data','eagle_sheep.jpg')])
# I'm dividing by 2 and adding 0.5 because of
# how this Inception represents images
plt.imshow(images[0] / 2 + 0.5)
plt.show()
preds = inet_model.predict(images)
for x in decode_predictions(preds)[0]:
print(x)
explainer = lime_image.LimeImageExplainer(feature_selection='none')#kernel_width=0.1 feature_selection='none'
# Hide color is the color for a superpixel turned OFF. Alternatively, if it is NONE, the superpixel will be replaced by the average of its pixels
explanation = explainer.explain_instance(images[0], inet_model.predict,
top_labels=1, hide_color=0, batch_size=10,
num_samples=100,model_regressor='non_Bay')#'non_Bay' 'Bay_non_info_prior' 'Bay_info_prior'
# temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=2, hide_rest=True)
# plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
# plt.show()
# temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=True, num_features=5, hide_rest=False)
# plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
# plt.show()
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=8, hide_rest=False)
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
plt.show()
LIME_time_2=np.array([])
i=5
while i<=1500:
n_samples=i
temp=timeit.timeit(stmt='the_LIME_interpretor(n_samples)',
setup='from __main__ import the_LIME_interpretor, n_samples',
number=1)
#print(temp[0], temp[1][0],temp[1][1])
LIME_time_2=np.append(LIME_time_2,temp)
i=i+55
i=5
x_index=np.array([])
while i<=1500:
x_index =np.append(x_index,i)
i=i+55
## Above is to rescale the x axis; Note need to have the same setup with the loop above...
plt.plot(x_index,LIME_time_2,linestyle='-',color='blue')
#plt.plot(x_index[w:],IPSP_est_upper[w:],linestyle='dotted',color='purple',label='IPSP upper bound')
#plt.axis([1, len(MCMC_time), 0, np.max(MCMC_time)])# set the ranges of axis
plt.legend(fontsize=10)#bbox_to_anchor=(1,1)
plt.xlabel('sample size $n$')
plt.ylabel('time (s)')
plt.grid(True)
plt.show()
images = transform_img_fn([os.path.join('..\data','dogs.jpg')])
# I'm dividing by 2 and adding 0.5 because of
# how this Inception represents images
plt.imshow(images[0] / 2 + 0.5)
plt.show()
preds = inet_model.predict(images)
for x in decode_predictions(preds)[0]:
print(x)
explainer = lime_image.LimeImageExplainer(feature_selection='none')#kernel_width=0.1 feature_selection='none'
i=1# do 3 times..
while i<=3:
# Hide color is the color for a superpixel turned OFF. Alternatively, if it is NONE, the superpixel will be replaced by the average of its pixels
explanation = explainer.explain_instance(images[0], inet_model.predict,
top_labels=1, hide_color=0, batch_size=10,
num_samples=100,model_regressor='non_Bay')#'non_Bay' 'Bay_non_info_prior' 'Bay_info_prior'
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=4, hide_rest=True)
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
plt.show()
i=i+1
images = transform_img_fn([os.path.join('..\data','5.jpg')])
# I'm dividing by 2 and adding 0.5 because of
# how this Inception represents images
plt.imshow(images[0] / 2 + 0.5)
plt.show()
preds = inet_model.predict(images)
for x in decode_predictions(preds)[0]:
print(x)
explainer = lime_image.LimeImageExplainer(feature_selection='none')#kernel_width=0.1 feature_selection='none'
i=1# do 3 times..
while i<=5:
# Hide color is the color for a superpixel turned OFF. Alternatively, if it is NONE, the superpixel will be replaced by the average of its pixels
explanation = explainer.explain_instance(images[0], inet_model.predict,
top_labels=1, hide_color=0, batch_size=10,
num_samples=100,model_regressor='non_Bay')#'non_Bay' 'Bay_non_info_prior' 'Bay_info_prior'
temp, mask = explanation.get_image_and_mask(explanation.top_labels[0], positive_only=False, num_features=4, hide_rest=False)
plt.imshow(mark_boundaries(temp / 2 + 0.5, mask))
plt.show()
i=i+1